package com.sphd.emanu.fragment.search;


import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.sphd.emanu.R;
import com.sphd.emanu.adapter.MyViewPagerAdapter;
import com.sphd.emanu.config.Constant;
import com.sphd.emanu.fragment.cardnum.CardNumFragment;
import com.sphd.emanu.fragment.time.TimeFragment;
import com.sphd.emanu.interfaces.CardFragmentLetter;
import com.sphd.emanu.interfaces.Letter;
import com.sphd.emanu.interfaces.LetterAtoF;
import com.sphd.emanu.mvp.MVPBaseFragment;
import com.sphd.emanu.pojo.MonthOrYearInfo;
import com.sphd.emanu.pojo.login.User;
import com.tuyenmonkey.mkloader.MKLoader;
import com.wzutils.TimeUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * MVPPlugin
 * 邮箱 784787081@qq.com
 */

public class SearchFragment extends MVPBaseFragment<SearchContract.View, SearchPresenter> implements SearchContract.View, Letter, CardFragmentLetter {

    @BindView(R.id.go_back_img)
    ImageView goBackImg;
    @BindView(R.id.financial_balance_text)
    TextView financialBalanceText;
    @BindView(R.id.financial_balance_hint)
    TextView financialBalanceHint;
    @BindView(R.id.income_text)
    TextView incomeText;
    @BindView(R.id.income_hint)
    TextView incomeHint;
    @BindView(R.id.spending_text)
    TextView spendingText;
    @BindView(R.id.spend_hint)
    TextView spendHint;
    @BindView(R.id.last_money_text)
    TextView lastMoneyText;
    @BindView(R.id.last_money_hint)
    TextView lastMoneyHint;
    @BindView(R.id.buttom_balance_linear)
    LinearLayout buttomBalanceLinear;
    @BindView(R.id.layout_last_month_balance)
    LinearLayout layoutLastMonthBalance;
    @BindView(R.id.show_time_cardnum_tablayout)
    TabLayout tabs;
    @BindView(R.id.vp_Fragment_pager)
    ViewPager vpFragmentPager;
    @BindView(R.id.loading_mkloader)
    MKLoader loadingMkloader;
    Unbinder unbinder;
    @BindView(R.id.my_toolbar)
    TextView myToolbar;
    Unbinder unbinder1;
    private List<Fragment> list_fragment;//定义要装fragment的列表
    CardNumFragment cardNumFragment;
    TimeFragment timeFragment;
    MyViewPagerAdapter myViewPagerAdapter;
    Bundle bundle;
    private String beginDate;
    private String endDate;
    User user;
    private int monthOrDay = 1;
    boolean timeOk = false;
    boolean cardOk = false;
    LetterAtoF letterAtoF;
    public void setLetterAtoF(LetterAtoF letterAtoF) {
        this.letterAtoF = letterAtoF;
    }
    @Override
    protected int getLayoutId() {
        return R.layout.fragment_search;
    }

    @Override
    protected void initView() {
        unbinder = ButterKnife.bind(this, rootView);
        bundle = getArguments();
        user = (User) bundle.getSerializable(Constant.USER_ENTITY);
        beginDate = bundle.getString(Constant.Salary_User_Start);
        endDate = bundle.getString(Constant.Salary_User_End);
        myToolbar.setText("搜索");
        //判端是日列表还是月列表
        monthOrDay = TimeUtils.isOneYear(beginDate, endDate) ?
                (TimeUtils.isOneMonth(beginDate, endDate) ? 2 : 1) : 1;
        //初始化显示方式
        initFragment();
        setUserVisibleHint(true);
    }


    private void initFragment() {
        list_fragment = new ArrayList<>();
        timeFragment = new TimeFragment();
        cardNumFragment = new CardNumFragment();
        bundle.putString(Constant.CLASS_NAME, SearchFragment.class.getName());
        timeFragment.setArguments(bundle);
        cardNumFragment.setArguments(bundle);
        timeFragment.setLetter(this);
        cardNumFragment.setCardFragmentLetter(this);
        list_fragment.add(timeFragment);
        list_fragment.add(cardNumFragment);
        String titles[] = {"按时间查看", "按账户查看"};
        myViewPagerAdapter = new MyViewPagerAdapter(context, getChildFragmentManager(), list_fragment, titles);
        tabs.setupWithViewPager(vpFragmentPager);
        vpFragmentPager.setAdapter(myViewPagerAdapter);
    }

    @Override
    protected void initData() {

    }

    private void addTitleData() {
        mPresenter.getAllAccountData(setMapMessage("4", beginDate, endDate, String.valueOf(monthOrDay)));
    }

    public Map setMapMessage(String state, String beginDate, String endDate, String monthOrDay) {
        Map<String, String> map = new HashMap<>();
        map.put("oid", String.valueOf(user.getOid()));
        map.put("state", state);
        map.put("beginDate", beginDate);
        map.put("endDate", endDate);
        map.put("monthOrDay", monthOrDay);
        return map;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @Override
    public void readyed() {
        timeOk = true;
        allOkAddData();
    }

    @Override
    public void setBean(MonthOrYearInfo monthOrYearInfo) {
        financialBalanceText.setText(monthOrYearInfo.getAllBalance() + "");
        incomeText.setText(monthOrYearInfo.getAllCredit() + "");
        incomeHint.setText("本月收入");
        spendingText.setText(monthOrYearInfo.getAllDebit() + "");
        spendHint.setText("本月支出");
        lastMoneyText.setText(monthOrYearInfo.getAllPreviousBalance() + "");
        lastMoneyHint.setText("上月余额");
        financialBalanceHint.setText("当前余额" + monthOrYearInfo.getBeginDate() + "~" + monthOrYearInfo.getEndDate());

        timeFragment.swipeRefreshData(4, 0, monthOrYearInfo.getBeginDate(), monthOrYearInfo.getEndDate());
        cardNumFragment.swipeRefreshData(4, monthOrYearInfo.getBeginDate(), monthOrYearInfo.getEndDate());
    }

    @Override
    public void readyedCardFragment() {
        cardOk = true;
        allOkAddData();
    }

    private void allOkAddData() {
        if (timeOk && cardOk) {
            addTitleData();
        }
    }



    @OnClick(R.id.go_back_img)
    public void onViewClicked() {
        letterAtoF.finishActivity();

    }
}
